package com.data.structure.linkedlist;

import java.util.Stack;

/**
 * 单链表
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/24
 */
public class DoubleLinkedListDemo {

    public static void main(String[] args) {

        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");
        DoubleLinkedList linkedList = new DoubleLinkedList();
        linkedList.add(hero1);
        linkedList.add(hero2);
        linkedList.add(hero3);
        linkedList.add(hero4);
        System.out.println("原来链表的情况~~");
        linkedList.list();
        System.out.println("测试逆序打印单链表, 没有改变链表的结构~~");
        reversePrint(linkedList.getHead());
        linkedList.del(4);
        System.out.println("删除~~");
        linkedList.list();
        System.out.println("有序添加~~");
        linkedList.addByOrder(hero4);
        linkedList.list();
    }

    /**
     * 逆序打印链表
     * @param head
     */
    public static void reversePrint(HeroNode2 head) {
        if (head.next != null) {
            Stack<HeroNode2> stack = new Stack();

            for(HeroNode2 cur = head.next; cur != null; cur = cur.next) {
                stack.push(cur);
            }

            while(stack.size() > 0) {
                System.out.println(stack.pop());
            }

        }
    }
}

/**
 * 单链表
 */
class DoubleLinkedList {

    /**
     * 头结点
     */
    private HeroNode2 head = new HeroNode2(0, "", "");

    /**
     * 尾节点
     */
    private HeroNode2 tail;

    DoubleLinkedList() {
        tail = head;
    }

    public HeroNode2 getHead() {
        return this.head;
    }

    public HeroNode2 getTail() {
        return this.tail;
    }

    /**
     * 修改
     * @param heroNode
     */
    public void add(HeroNode2 heroNode) {

        HeroNode2 temp = tail;
        temp.next = heroNode;
        heroNode.pre = temp;
        tail = heroNode;
    }

    /**
     * 有序添加
     * @param heroNode
     */
    public void addByOrder(HeroNode2 heroNode) {
        HeroNode2 temp = this.head;

        boolean flag;
        for(flag = false; temp.next != null && temp.next.no <= heroNode.no; temp = temp.next) {
            if (temp.next.no == heroNode.no) {
                flag = true;
                break;
            }
        }

        if (flag) {
            System.out.printf("准备插入的英雄的编号 %d 已经存在了, 不能加入\n", heroNode.no);
        } else {
            if(null != temp.next){
                heroNode.next = temp.next;
                temp.next.pre = heroNode;
            }else{
                tail = heroNode;
            }
            heroNode.pre = temp;
            temp.next = heroNode;
        }

    }

    /**
     * 修改
     * @param newHeroNode
     */
    public void update(HeroNode2 newHeroNode) {
        if (this.head.next == null) {
            System.out.println("链表为空~");
        } else {
            HeroNode2 temp = this.head.next;

            boolean flag;
            for(flag = false; temp != null; temp = temp.next) {
                if (temp.no == newHeroNode.no) {
                    flag = true;
                    break;
                }
            }

            if (flag) {
                temp.name = newHeroNode.name;
                temp.nickname = newHeroNode.nickname;
            } else {
                System.out.printf("没有找到 编号 %d 的节点，不能修改\n", newHeroNode.no);
            }

        }
    }

    /**
     * 删除节点
     * @param no
     */
    public void del(int no) {
        HeroNode2 temp = this.head;

        boolean flag;
        for(flag = false; temp != null; temp = temp.next) {
            if (temp.no == no) {
                flag = true;
                break;
            }
        }

        if (flag) {
            if (null !=  temp.next){
                temp.next.pre = temp.pre;
                temp.pre.next = temp.next;
            }else{
                temp.pre.next = null;
                tail = temp.pre;
                //temp = null;
            }
        } else {
            System.out.printf("要删除的 %d 节点不存在\n", no);
        }

    }

    /**
     *遍历
     */
    public void list() {
        if (this.head.next == null) {
            System.out.println("链表为空");
        } else {
            for(HeroNode2 temp = this.head.next; temp != null; temp = temp.next) {
                System.out.println(temp);
            }

        }
    }

}

/**
 * 链表节点
 */
class HeroNode2 {

    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
    }

}